<style>
.ivu-select-dropdown {
  z-index: 9999;
}
</style>

<template>
  <Row>
    <dtable 
      :columns="columns" 
      :data-all="dataAll" 
      :data-show="dataShow" 
      :totals="total" 
      :loading="loading" 
      add 
      del-check 
      check 
      op 
      @sortOrder="sortOrder" 
      @refresh="getall(pagedata)" 
      @add="add" 
      @del="del" 
      @edit="edit" 
      @delCheck="delCheck" 
      @changePage="changePage"
    />
    <Modal 
      v-model="editData" 
      title="用户信息" 
      width="1000" 
      :mask-closable="false" 
      class="editm" 
      @on-ok="asyncOK" 
      @on-cancel="asyncCancel"
    >
      <Form 
        ref="editForm" 
        :model="formItem" 
        :label-width="80" 
        :rules="ruleValidate"
      >
        <Form-item 
          label="公告标题" 
          prop="Title"
        >
          <Input 
            v-model="formItem.Title" 
            placeholder="请输入" 
            :disabled="addid" 
            @on-enter="asyncOK()"
          ></Input>
        </Form-item>
        <Form-item 
          label="发布范围" 
          prop="Type"
        >
          <Radio-group v-model.number="formItem.Type">
            <Radio 
              v-if="getScope" 
              label="2"
            >全系统</Radio>
            <Radio label="1">全单位</Radio>
            <Radio label="0">本部门</Radio>
          </Radio-group>
        </Form-item>
        <Form-item 
          label="生效控制" 
          prop="Btime"
        >
          <Date-picker 
            v-model="formItem.Rtime" 
            type="daterange" 
            :options="optionsDate" 
            placement="bottom-end" 
            placeholder="选择日期"
          />

        <!--<Date-picker type="daterange" placement="bottom-end" :options="optionsDate" placeholder="选择日期"  v-model="formItem.Rtime" sytle="z-index: 9999;"></Date-picker>-->

        </Form-item>
        <Form-item 
          label="公告内容" 
          prop="Content"
        >
          <vue-html5-editor 
            ref="editor" 
            :content="formItem.Content" 
            :height="200" 
            @change="updateData"
          />
        </Form-item>
      </Form>
    </Modal>
  </Row>
</template>

<script>
import dtable from "@/components/table/btable.vue"; //采用biztable
import moment from "moment";
import { timeFormat } from "@/common/filter";
export default {
  components: {
    dtable
  },
  data() {
    return {
      loading: false, //数据刷新遮罩层
      columns: [
        {
          title: "标题",
          key: "Title",
          width: 140,
          sortable: true
        },
        {
          title: "类别",
          key: "Tname",
          sortable: true
        },
        {
          title: "所属单位",
          key: "OrgName",
          sortable: true
        },
        {
          title: "部门",
          key: "Dname",
          sortable: true
        },
        {
          title: "生效时间",
          key: "Btime",
          sortable: true,
          render: (h, params) => {
            return h("div", [h("strong", timeFormat(params.row.Btime))]);
          }
        },
        {
          title: "失效时间",
          key: "Etime",
          sortable: true,
          render: (h, params) => {
            return h("div", [h("strong", timeFormat(params.row.Etime))]);
          }
        },

        {
          title: "创建者",
          key: "Updater",
          sortable: true
        },
        {
          title: "发布时间",
          key: "Utime",
          sortable: true,
          render: (h, params) => {
            return h("div", [h("strong", timeFormat(params.row.Utime))]);
          }
        }
      ], //表格样式
      dataAll: [], //表格数据来源
      dataShow: true, //是否显示表格数据查看表单
      editData: false, //显示编辑表单
      formItem: {
        Title: "",
        Btime: "",
        Etime: "",
        Rtime: [],
        Type: 0,
        OrgId: "",
        OrgName: "",
        Status: 1,
        Content: ""
      }, //数据编辑表单
      ruleValidate: {
        Title: [
          {
            required: true,
            message: "标题不能为空",
            trigger: "blur"
          }
        ],
        Rtime: [
          {
            required: true,
            type: "date",
            message: "生效日期不能为空",
            trigger: "change"
          }
        ]
      }, //数据编辑表单验证规则
      addid: true, //true为修改模式，false为添加模式
      role: [],
      org: [],
      total: 0,
      pagedata: {
        pagesize: 10,
        page: 1,
        searchKey: "",
        sortby: ["Ctime"],
        order: ["desc"]
      },
      optionsDate: {
        shortcuts: [
          {
            text: "最近一周",
            value() {
              const end = new Date();
              const start = new Date();
              end.setTime(end.getTime() + 3600 * 1000 * 24 * 7);
              return [start, end];
            }
          },
          {
            text: "最近一个月",
            value() {
              const end = new Date();
              const start = new Date();
              end.setTime(end.getTime() + 3600 * 1000 * 24 * 30);
              return [start, end];
            }
          },
          {
            text: "最近三个月",
            value() {
              const end = new Date();
              const start = new Date();
              end.setTime(end.getTime() + 3600 * 1000 * 24 * 90);
              return [start, end];
            }
          }
        ]
      }
    };
  },
  computed: {
    state() {
      return this.$store.state.app;
    },
    getRtime() {
      let rtime = [];
      rtime.push(this.formItem.Btime, this.formItem.Etime);
      return rtime;
    },
    getScope() {
      // console.log(this.state.userInfo.Role[0].Type)
      if (this.state.role.Type && this.state.role.Type > 0) {
        return false;
      } else {
        return true;
      }
    }
  },
  watch: {},
  mounted() {
    this.getall(this.pagedata);
    // self.getOrganization()
    // self.getRole()
  },
  methods: {
    //富文本框内容对应
    updateData(data) {
      // sync content to component
      this.formItem.Content = data;
    },
    //      排序
    sortOrder(current) {
      console.log(current);
      this.pagedata = current;
      this.getall(this.pagedata);
    },
    //     获取资源名称
    getNameById(value) {
      let rname = "";
      if (value || "") {
        let rv = this.dataAll.filter((col, index) => col.Id === value);
        rname = rv[0].Name;
      }
      return rname;
    },
    //      改变页码
    changePage(pagedata) {
      this.spinShow = true;
      this.$api.getBulletin(pagedata).then(res => {
        if (!res.error) {
          this.total = res.Data.total;
          this.dataAll = res.Data.tabledata;
        } else {
          this.$Message.error(res.message);
        }
      });
      setTimeout(() => {
        this.spinShow = false;
      }, 500);
    },
    getall(pg) {
      this.loading = true;
      this.$api.getBulletin(pg).then(res => {
        if (!res.error) {
          this.total = res.Data.total;
          this.dataAll = res.Data.tabledata;
        } else {
          this.$Message.error(res.message);
        }
        this.loading = false;
      });
    },
    // getOrganization() {
    //   // this.spinShow = true
    //   this.$api.getOrganizationList().then(res => {
    //     if (!res.error) {
    //       this.org = res.Data
    //     } else {
    //       this.$Message.error(res.message)
    //     }
    //   }, err => {
    //     this.$Message.error(res.message, 4)
    //   })
    // },
    // getRole() {
    //   // this.spinShow = true
    //   this.$api.GetOrgRoleList().then(res => {
    //     if (!res.error) {
    //       this.role = res.Data
    //     } else {
    //       this.$Message.error(res.message)
    //     }
    //   }, err => {
    //     this.$Message.error(res.message, 4)
    //   })
    // },
    edit(index) {
      this.addid = true;
      this.formItem = index;
      this.formItem.Rtime = this.getRtime;
      this.editData = true;
    },
    add() {
      this.addid = false;
      this.$refs["editForm"].resetFields();
      this.formItem.Rtime = this.getRtime;
      this.editData = true;
    },
    asyncOK() {
      this.formItem.Btime = moment(this.formItem.Rtime[0]).format("YYYY-MM-DD");
      this.formItem.Etime = moment(this.formItem.Rtime[1]).format("YYYY-MM-DD");
      this.$refs["editForm"].validate(valid => {
        if (valid) {
          if (this.addid) {
            this.$api.updateBulletin(this.formItem).then(
              res => {
                if (!res.error) {
                  this.getall(this.pagedata);
                  this.$Message.success(res.message);
                } else {
                  this.$Message.error(res.message, 4);
                }
              },
              err => {
                this.$Message.error(res.message, 4);
              }
            );
          } else if (!this.addid) {
            this.$api.addBulletin(this.formItem).then(
              res => {
                if (!res.error) {
                  this.getall(this.pagedata);
                  this.$Message.success(res.message);
                } else {
                  this.$Message.error(res.message, 4);
                }
              },
              err => {
                this.$Message.error(res.message, 4);
              }
            );
          }
          setTimeout(() => {
            this.editData = false;
          }, 1000);
        } else {
          this.$Message.error("表单验证失败!");
        }
      });
      this.editData = false;
      this.$refs["editForm"].resetFields();
    },
    asyncCancel() {
      this.getall(this.pagedata);
      this.$refs["editForm"].resetFields();
    },
    del(row) {
      api.deleteBulletin(row).then(
        res => {
          if (!res.error) {
            this.getall(this.pagedata);
            this.$Message.success(res.message);
          } else {
            this.$Message.error(res.message, 4);
          }
        },
        err => {
          this.$Message.error(res.message, 4);
        }
      );
    },
    delCheck(row) {
      this.$api.deleteBulletins(row).then(
        res => {
          if (!res.error) {
            this.getall(this.pagedata);
            this.$Message.success(res.message);
          } else {
            this.$Message.error(res.message, 4);
          }
        },
        err => {
          this.$Message.error(res.message, 4);
        }
      );
    }
  },
};
</script>
